﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace MemberManager.Common.Classes
{
    public class DomainHelper : BaseHelper
    {

        public List<string> GetList()
        {

            if (_application == null || _application.Domains == null || _application.Domains.Count == 0)
            {
                return null;
            }
            else if (_application.Domains == null || _application.Domains.Count == 0)
            {
                return null;
            }
            List<string> list = new List<string>();

            for(int x = 0;x<_application.Domains.Count;x++)
            {
                list.Add(_application.Domains[x].Name);
            }
            return list;
        }

        /// <summary>
        /// 添加域名
        /// </summary>
        /// <param name="domainName">域名</param>
        /// <param name="maxNumberOfAccounts">每个域中的最大帐号数量</param>
        /// <param name="maxSpace">域空间大小</param>
        /// <returns></returns>
        public bool AddDomain(string domainName,bool isCreateMaster = false, string masterPwd = "")
        {
            hMailServer.Domains domains = _application.Domains;

            //判断域名是否存在,这里的try是为了避免 ItemByName找不到时候的错误

            hMailServer.Domain domain = null;
            try
            {
                domain = domains.ItemByName[domainName];
            }
            catch
            {
                
            }

            if (domain != null)
                return false;



            domain = domains.Add();
            Marshal.ReleaseComObject(domains);

            domain.Name = domainName;
            domain.Postmaster = "";

            //激活
            domain.Active = true;


            domain.MaxNumberOfAliases = 0;
            domain.MaxNumberOfDistributionLists = 0;


            domain.MaxNumberOfAliasesEnabled = false;
            domain.MaxNumberOfDistributionListsEnabled = false;


            domain.SignaturePlainText = "";
            domain.SignatureHTML = "";

            domain.PlusAddressingEnabled = false;
            domain.PlusAddressingCharacter = "";
            domain.AntiSpamEnableGreylisting = false;

            domain.DKIMSignEnabled = false;
            domain.DKIMPrivateKeyFile = "";
            domain.DKIMSelector = "";



            try
            {
                domain.Save();

                if(isCreateMaster)
                {
                    //创建帐号
                    AccountHelper ah = new AccountHelper();

                    if(!ah.AddAccount(domainName, "PostMaster", masterPwd, hMailServer.eAdminLevel.hAdminLevelDomainAdmin))
                    {
                        return false;
                    }
                }
                

                return true;
            }
            catch (COMException ex)
            {
                if (ex.Message == "Failed to save object. Another domain with this name already exists.")
                {
                    return false;
                }
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
    }
}
